package com.example.cash.craft.controller;

import com.example.cash.craft.pojo.core.CurrentPrincipal;
import com.example.cash.craft.pojo.vo.PageData;
import com.example.cash.craft.pojo.vo.RewardPointsLogVO;
import com.example.cash.craft.service.IRewardPointsLogService;
import com.example.cash.craft.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "6. 积分管理模块")
@RestController
@RequestMapping("/api/reward-points-log")
public class RewardPointsLogController {

    @Autowired
    private IRewardPointsLogService rewardPointsLogService;

    @Operation(summary = "[用户] 查询会员积分（实时）")
    @ApiOperationSupport(order = 400)
    @GetMapping("/query-sum")
    public JsonResult<Integer> listByUser(@AuthenticationPrincipal CurrentPrincipal currentPrincipal) {
        Integer sumByUserId = rewardPointsLogService.sumByUserId(currentPrincipal.getId());
        return JsonResult.ok(sumByUserId);
    }

    @Operation(summary = "[用户] 查询积分记录日志")
    @ApiOperationSupport(order = 410)
    @Parameter(name = "page", description = "页码", example = "1", required = false)
    @GetMapping()
    public JsonResult<PageData<RewardPointsLogVO>> listByUser(Integer page,
                                                              @AuthenticationPrincipal CurrentPrincipal currentPrincipal) {
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<RewardPointsLogVO> data = rewardPointsLogService.listByUser(page, currentPrincipal);
        return JsonResult.ok(data);
    }

}